// <!--
//  * 严肃声明：
//  * 开源版本请务必保留此注释头信息，若删除我方将保留所有法律责任追究！
//  * 可正常分享和学习源码，不得用于违法犯罪活动，违者必究！
//  * Copyright (c) 2022 APThink David唐 all rights reserved.
//  * 版权所有，侵权必究！
//  *
// -->
// 在你的 setup 方法中
import 'element-plus/es/components/message/style/css'
import 'element-plus/es/components/message-box/style/css'
import 'element-plus/es/components/notification/style/css'

import { ElMessage, ElMessageBox, ElNotification } from "element-plus"
import { i18n } from "../i18n"
import { h } from "vue"
import log from "loglevel"

export function notification(title: string, message: string) {
  ElNotification({
    title: title,
    message: h('i', { style: 'color: teal' }, message),
  })  
}

export function errorMessage(data: any) {
  log.debug(data)
  const msg = i18n.global.t("response." + data.errorid)
  toast(msg, "error")
}

export function serviceMessage(data: any) {
  log.debug(data)
  const s = i18n.global.t("service." + data.id)
  let msg
  if (data.id < 1000) {
    if (data.id == 100) {
      msg = s.replace(/%s/, data.data)
    } else if (data.id == 101) {
      msg = s.replace(/%s/, i18n.global.t("table." + data.data.toLowerCase()))
    } else if (data.id == 99) {
      msg = data.message
    } else if (data.id == 197 || data.id == 198) {
      msg = s.replace(/%s/, data.data)
    } else if (data.id == 199) {
      msg = []
      msg.push(s)
      data.data.forEach((x: any) => {
        const s =
          i18n.global.t("table." + x.tablename.toLowerCase()) +
          "(" +
          i18n.global.t("column." + x.fieldname.toLowerCase()) +
          "): " +
          x.count
        msg.push(s)
      })
    } else {
      msg = s.replace(/%s/, i18n.global.t("label." + data.data))
    }
  } else {
    if (data.id == 10005) msg = s + "(" + data.message + ")"
    else msg = s
  }
  return toast(msg, "error")
}

/**
 * [message description]
 * @param  {[type]} msg [description]
 * @return {[type]}     [description]
 */
export function message(msg: string | Array<string>, type: string) {
  const s = []
  if (msg instanceof Array) {
    const t = []
    s.push(h("b", {}, msg[0]))
    for (let i = 1, l = msg.length; i < l; i++) {
      t.push(h("li", {}, msg[i]))
    }
    s.push(h("ol", {}, t))
  } else {
    s.push(h("div", {}, msg.toString))
  }
  const info = h("div", {}, s)
  ElMessageBox({
    title: i18n.global.t("form.informationtitle"),
    message: info,
    confirmButtonText: i18n.global.t("button.close"),
    type: type ? type : "success",
    draggable: true,
  }).then(() => { })
}

/**
 * [confirm description]
 * @param  {[type]} msg [description]
 * @return {[type]}     [description]
 */
export function confirm(msg: string) {
  // return new Promise(function (resolve, reject) {
  return new Promise(function (resolve) {
    ElMessageBox.confirm(msg, i18n.global.t("dialog.confirmtitle"), {
      confirmButtonText: i18n.global.t("button.confirm"),
      cancelButtonText: i18n.global.t("button.cancel"),
      type: "warning",
      draggable: true,
    })
      .then(() => {
        resolve(null)
      })
      .catch(() => {
        log.debug("click cancel!")
      })
  })
}

export function toast(msg: string, type?: string) {
  ElMessage({
    message: msg,
    type: type ? type : "success",
    grouping: true,
    duration: 3000,
  })
}

//全屏
export function fullScreen() {
  const el = document.documentElement as any
  const rfs =
    el.requestFullScreen ||
    el.webkitRequestFullScreen ||
    el.mozRequestFullScreen ||
    el.msRequestFullscreen
  if (typeof rfs != "undefined" && rfs) {
    rfs.call(el)
  }
  return
}
//退出全屏
export function exitScreen() {
  if (document.exitFullscreen) {
    document.exitFullscreen()
  } else if (document.mozCancelFullScreen) {
    document.mozCancelFullScreen()
  } else if (document.webkitCancelFullScreen) {
    document.webkitCancelFullScreen()
  } else if (document.msExitFullscreen) {
    document.msExitFullscreen()
  }
}
